package com.yx.changdao.web.controller;

import com.yx.changdao.common.entity.Article;
import com.yx.changdao.common.entity.ArticleCategory;

import com.yx.changdao.common.utils.UploadUtils;
import com.yx.changdao.common.vo.ArticleCategoryVo;
import com.yx.changdao.service.ArticleCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.ArticleService;
import com.yx.changdao.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: <br/>
 * 文章分类
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/04/10 14:49
 */
@RestController
@RequestMapping("article/category")
public class ArticleCategoryController {

    /**
     * 文章分类服务
     */
    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    public Result list() throws AlertException {

        List<ArticleCategory> list = articleCategoryService.list(SysUtils.autoCnd(ArticleCategory.class));
        /**
         * 图片地址处理
         */
        list.forEach(t -> {
            t.setImg(UploadUtils.advanceShow(t.getImg()));
        });

        List<ArticleCategoryVo> listVo=new ArrayList<>();
        for (ArticleCategory articleCategory : list) {
            ArticleCategoryVo vo=new ArticleCategoryVo();
            List<Article> list1 = articleService.selectList(new QueryWrapper<Article>()
                    .eq(Article.CATEGORY_ID, articleCategory.getId()).orderByDesc(Article.CREATE_TIME));
            /*List<Article> list1 = articleService.list(new QueryWrapper<Article>()
                    .eq(Article.CATEGORY_ID, articleCategory.getId()).orderByDesc(Article.CREATE_TIME));*/
            vo.setArticles(list1);
            vo.setId(articleCategory.getId());
            vo.setImg(articleCategory.getImg());
            vo.setName(articleCategory.getName());
            listVo.add(vo);
        }

        return Result.ok().data(listVo);
    }


    /**
     * 添加文章分类
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
    public Result add(@RequestBody @Valid ArticleCategory articleCategory) throws AlertException {
        /**
         * 图片地址预处理
         */
        articleCategory.setImg(UploadUtils.advanceSave(articleCategory.getImg()));
        boolean b = articleCategoryService.save(articleCategory);

        if (!b) {
            throw new AlertException("文章分类添加失败");
        }

        return Result.ok("文章分类添加成功");
    }


    /**
     * 获得指定id 文章分类。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id) throws AlertException {
        ArticleCategory articleCategory = articleCategoryService.getById(id);

        if (articleCategory == null) {
            throw new AlertException("未找到相关文章分类信息");
        }

        return Result.ok().data(articleCategory);
    }


    /**
     * 更新文章分类
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PutMapping
    public Result update(@RequestBody @Valid ArticleCategory articleCategory) throws AlertException {
        List<Article> articleList = articleService.list
                (new QueryWrapper<Article>().eq(Article.CATEGORY_ID, articleCategory.getId()));

        if (articleList.size() != 0) {
            for (Article article : articleList) {
                article.setName(articleCategory.getName());
                articleService.updateById(article);
            }
        }

        /**
         * 图片地址预处理
         */
        articleCategory.setImg(UploadUtils.advanceSave(articleCategory.getImg()));
        boolean b = articleCategoryService.updateById(articleCategory);
        if (!b) {
            throw new AlertException("文章分类更新失败");
        }

        return Result.ok("文章分类更新成功");
    }


    /**
     * 删除文章分类
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) throws AlertException {
        List<Article> list = articleService.list(new QueryWrapper<Article>().eq(Article.CATEGORY_ID, id));
        if (list.size() != 0) {
            for (Article article : list) {
                articleService.removeById(article.getId());
            }
        }
        boolean b = articleCategoryService.removeById(id);
        if (!b) {
            throw new AlertException("文章分类删除失败");
        }

        return Result.ok("删除文章分类成功");
    }

}
